package com.synjones.zeuscloudapp.util;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
//import sun.misc.BASE64Decoder;
//import sun.misc.BASE64Encoder;


/**
 *
 * @author Administrator
 */
public class AESUtils {

    private static final String AESTYPE = "AES/CBC/PKCS5Padding";
    public static String ivParameter = "1234567890123456";

    /**
     * 此处加密，为保证http传输正确性，采用Base64.getUrlEncoder()编码
     *
     * @param plainText
     * @param keyStr
     * @return
     */
//    public static String encrypt2Str(String plainText, String keyStr) {
//        byte[] encrypt = null;
//        try {
//            Key key = generateKey(keyStr);
//            Cipher cipher = Cipher.getInstance(AESTYPE);
//            cipher.init(Cipher.ENCRYPT_MODE, key);
//            encrypt = cipher.doFinal(plainText.getBytes(Charsets.UTF_8));
//        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
//            throw new RuntimeException("加密出错！", e);
//        }
//        return Base64.getUrlEncoder().encodeToString(encrypt);
//    }
//
//    /**
//     * 此处解密，为保证http传输正确性，采用Base64.getUrlDecoder()编码
//     * @param encryptData
//     * @param keyStr
//     * @return
//     */
//    public static String decrypt(String encryptData, String keyStr) {
//        byte[] decrypt = null;
//        try {
//            Key key = generateKey(keyStr);
//            Cipher cipher = Cipher.getInstance(AESTYPE);
//            cipher.init(Cipher.DECRYPT_MODE, key);
//            decrypt = cipher.doFinal(Base64.getUrlDecoder().decode(encryptData));
//        } catch (InvalidKeyException | NoSuchAlgorithmException | BadPaddingException | IllegalBlockSizeException | NoSuchPaddingException e) {
//            throw new RuntimeException("解密出错！", e);
//        }
//        return new String(decrypt, Charsets.UTF_8).trim();
//    }
//
//    private static Key generateKey(String key) {
//        try {
//            SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");
//            return keySpec;
//        } catch (Exception e) {
//            throw new RuntimeException("获取key出错！", e);
//        }
//
//    }
    // 加密
    public static String encrypt(String encryptData, String keyStr) {
        try {
            Cipher cipher = Cipher.getInstance(AESTYPE);
            byte[] raw = keyStr.getBytes();
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());//使用CBC模式，需要一个向量iv，可增加加密算法的强度
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
            byte[] encrypted = cipher.doFinal(encryptData.getBytes("UTF-8"));
//            return new Base64().encode(encrypted);
            return Base64.getEncoder().encodeToString(encrypted);
        } catch (Exception e) {
            return "";
        }
    }

    // 解密
    public static String decrypt(String encryptData, String keyStr) {
        try {
            byte[] raw = keyStr.getBytes("ASCII");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance(AESTYPE);
            IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
            encryptData = encryptData.replaceAll("-", "+");
            encryptData = encryptData.replaceAll("_", "/");
            byte[] encrypted1 = Base64.getDecoder().decode(encryptData);//先用base64解密
            byte[] original = cipher.doFinal(encrypted1);
            String originalString = new String(original, "UTF-8");
            return originalString;
        } catch (Exception ex) {
            return "";
        }
    }

    public static void main(String[] args) {
        String temp = encrypt("11111", "1111111111111111");
        System.out.println(temp);

        String temp1 = decrypt("YwnkQRvDGyyvPnhwJP8EoPvA9kVRBuZanDVSkaYs3j5p59/jxZLjeFGCBlI5Cx5RdoMS8nKgcf9/CfgUJ8fXKW3xaOg5gnr90U4IbNGEnvgSuSot7E/KtYGslV9hKRxNECQNzW0Nq5TSGRefxKymvI9TLnzdITih/FLJdgWbFj+gaENtUwyHd7/uMqUMYa8jz0JFhL55EJCq3cyiM0NhahGWL3eH4moopc1rF0Bfa53ziirCnOIRMjEaJFE3PuoZ", "1111111111111111");
        System.out.println(temp1);

        /*
         * 解密
         */

    }

}
